package com.ymatou.promotion.api.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.CouponTitle;
import com.ymatou.promotion.api.parameter.GetAllSellerCouponListBean;
import com.ymatou.promotion.api.parameter.SellerCouponValue;
import com.ymatou.promotion.api.parameter.SellerCouponValueList;
import com.ymatou.promotion.api.service.GetAllSellerCouponListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;

/**
 * 获取所有买手优惠券集合接口
 * 
 * @author chenjiazhu
 */
@TestSuite
public class Ts_GetAllSellerCouponList {
    private static GetAllSellerCouponListBean getallsellercouponlistBean;
    private static GetAllSellerCouponListCall getallsellercouponlistCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取所有买手优惠券集合接口");
    }

    @Before
    public void caseUp() {
        getallsellercouponlistBean = new GetAllSellerCouponListBean();
        getallsellercouponlistCall = new GetAllSellerCouponListCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getallsellercouponlistCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetAllSellerCouponList_001() {
        Logger.start(true, "获取所有买手优惠券集合");
        try {

            getallsellercouponlistCall.setData(getallsellercouponlistBean);
            getallsellercouponlistCall.callService();

            Logger.verifyEquals("200", getallsellercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals("0", getallsellercouponlistCall.getBCode(),
                    "验证返回Bcode");

            SellerCouponValueList sellerCouponValueList = getallsellercouponlistCall.getSellerCouponValueList();

            for (SellerCouponValue sellerCouponValue : sellerCouponValueList.getSellerCouponValueList()) {
                YmtCouponBatch batch = new
                        YmtCouponBatchWapper().selectByPrimaryKey(sellerCouponValue.getBatchId());
                Logger.verifyEquals(batch.getsBatchCode(),
                        sellerCouponValue.getBatchCode(),
                        "验证返回BatchCode与数据库一致");

                Logger.verifyEquals(batch.getiOperatorId(),
                        sellerCouponValue.getSellerId(),
                        "验证返回SellerId与数据库一致");

                Logger.verifyEquals(batch.getiCouponSettingId(),
                        sellerCouponValue.getCouponSettingId(),
                        "验证返回CouponSettingId与数据库一致");

                YmtCouponSetting setting =
                        new
                        YmtCouponSettingWapper().selectByiCouponSettingId(sellerCouponValue.getCouponSettingId())
                                .get(0);

                // 验证ValidStart，ValidEnd
                Logger.verifyEquals(getDateString(setting.getdValidStart()),
                        getDateString2(sellerCouponValue.getValidStart()),
                        "验证返回ValidStart与数据库表Ymt_CouponSetting中字段dValidStart一致");
                Logger.verifyEquals(getDateString(setting.getdValidEnd()),
                        getDateString2(sellerCouponValue.getValidEnd()),
                        "验证返回ValidEnd与数据库表Ymt_CouponSetting中字段dValidEnd一致");

                Logger.verifyEquals(setting.getiCouponUseType(),
                        sellerCouponValue.getCouponUseType(),
                        "验证返回CouponUseType与数据库一致");

                Logger.verifyEquals(setting.getiMaxUseTimePerUser(),
                        sellerCouponValue.getMaxUseTimePerUser(),
                        "验证返回MaxUseTimePerUser与数据库一致");

                YmtCouponValue couponValue =
                        new YmtCouponValueWapper().selectByiCouponSettingId(sellerCouponValue.getCouponSettingId())
                                .get(0);

                Logger.verifyEquals(0,
                        sellerCouponValue.getMinOrderValue().compareTo(couponValue.getFminordervalue()),
                        "验证返回MinOrderValue与数据库一致");

                Logger.verifyEquals(0,
                        sellerCouponValue.getCouponValue().compareTo(couponValue.getFcouponvalue()),
                        "验证返回MinOrderValue与数据库一致");
            }

            for (CouponTitle couponTitle : sellerCouponValueList.getCouponTitleList()) {
                int settingId = couponTitle.getCouponSettingId();

                YmtCouponValue couponValue =
                        new YmtCouponValueWapper().selectByiCouponSettingId(settingId)
                                .get(0);

                Logger.verifyEquals("订单满"
                        + couponValue.getFminordervalue().setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "元使用",
                        couponTitle.getTitle(),
                        "验证返回MinOrderValue与数据库一致");
            }

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private String getDateString(Date date) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");

        return formatter1.format(date) + "T" + formatter2.format(date);
    }

    private String getDateString2(String date) {
        if (date.length() != 19) {
            date = date.substring(0, 19);
        }

        return date;
    }

    // 与数据库返回做验证，但是逻辑经常变，所以
    private void verifyAllReturnWithDB()
    {
        List<SellerCouponValue> sellerCouponValueList = getallsellercouponlistCall
                .getSellerCouponValueList().getSellerCouponValueList();

        List<Map> result = new YmtCouponBatchWapper()
                .getAllSellerCouponList();

        Logger.verifyEquals(result.size(), sellerCouponValueList.size(),
                "验证返回优惠券条数正确");

        for (int i = 0; i < sellerCouponValueList.size(); i++) {
            // for (int i = 0; i < 2; i++) {

            Logger.comment("验证第" + i + "条记录");

            YmtCouponBatch newresult =
                    new
                    YmtCouponBatchWapper().selectBysBatchCode(sellerCouponValueList.get(i).getBatchCode()).get(
                            0);

            Logger.verifyEquals(newresult.getiOperatorId(), sellerCouponValueList
                    .get(i).getSellerId(), "验证返回SellerId与数据库一致");
            Logger.verifyEquals(newresult.getiBatchId(),
                    sellerCouponValueList.get(i).getBatchId(),
                    "验证返回BatchId与数据库一致");
            Logger.verifyEquals(newresult.getiCouponSettingId(),
                    sellerCouponValueList.get(i).getCouponSettingId(),
                    "验证返回CouponSettingId与数据库一致");

            Logger.verifyEquals(
                    0,
                    sellerCouponValueList
                            .get(i)
                            .getMinOrderValue()
                            .compareTo(
                                    new BigDecimal(result.get(i)
                                            .get("MinOrderValue")
                                            .toString())),
                    "验证返回MinOrderValue与数据库一致");
            Logger.verifyEquals(
                    0,
                    sellerCouponValueList
                            .get(i)
                            .getCouponValue()
                            .compareTo(
                                    new BigDecimal(result.get(i)
                                            .get("CouponValue").toString())),
                    "验证返回CouponValue与数据库一致");
            Logger.verifyEquals(Integer.parseInt(result.get(i)
                    .get("CouponUseType").toString()),
                    sellerCouponValueList.get(i).getCouponUseType(),
                    "验证返回CouponUseType与数据库一致");

            Logger.verifyEquals(
                    getDateString((Date) result.get(i).get("ValidStart")),
                    getDateString2(sellerCouponValueList.get(i)
                            .getValidStart()), "验证返回ValidStart与数据库一致");
            Logger.verifyEquals(
                    getDateString((Date) result.get(i).get("ValidEnd")),
                    getDateString2(sellerCouponValueList.get(i)
                            .getValidEnd()), "验证返回ValidEnd与数据库一致");

        }
    }

}
